﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using OilRigSumoWrestlers.Scenes.Menu;
using RaptorEngine;
using RaptorEngine.Components;

namespace OilRigSumoWrestlers.Controllables
{
	class TestControllable : Controllable
	{
		private Movable _mov;

		public TestControllable(Entity entity)
			: base(entity)
		{
		}

		public override void Update(GameTime gameTime)
		{
            if (GameEngine.Instance.Input.CommandActive((int)InputType.Menu))
		    {
                GameEngine.Instance.Scene.StopCurrentScene();
                GameEngine.Instance.Scene.PushScene(new PauseMenu());
                GameEngine.Instance.Scene.StartCurrentScene();
                GameEngine.Instance.Scene.ShowCurrentScene();
            }

			
			if (Entity.HasComponent(ComponentType.Movable))
			{
				_mov = (Movable) Entity.GetComponent(ComponentType.Movable);
			
				// Handle movement on the X-axis
                if (GameEngine.Instance.Input.IsDown(Keys.Left))
				{
					MoveLeft();
				}
                else if (GameEngine.Instance.Input.IsDown(Keys.Right))
				{
					MoveRight();
				}
				else
				{
					_mov.Acceleration = new Vector3(0, _mov.Acceleration.Y, _mov.Acceleration.Z);
				}

				// Handle movement on the Y-axis
                if (GameEngine.Instance.Input.IsDown(Keys.Space))
				{
					Jump();
				}
				else
				{
					_mov.Acceleration = new Vector3(_mov.Acceleration.X, 0, _mov.Acceleration.Z);
				}
				
				// Handle movement on the Z-axis
				if (GameEngine.Instance.Input.IsDown(Keys.Up))
				{
					MoveBack();
				}
				else if (GameEngine.Instance.Input.IsDown(Keys.Down))
				{
					MoveFront();
				}
				else
				{
					_mov.Acceleration = new Vector3(_mov.Acceleration.X, _mov.Acceleration.Y, 0);
				}
			}
			GameEngine.Instance.Debug.SetData("Entity" + Entity.Id + " pos", Entity.Position.ToString()); 
		}

		private void Jump()
		{
			if (_mov.Velocity.Y <= 0.01)
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, GameConstant.PLAYER_JUMP, _mov.Acceleration.Z);
			}
			else
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, 0, _mov.Acceleration.Z);
			}
		}

		private void MoveLeft()
		{
			if (_mov.Velocity.X > -GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(-GameConstant.PLAYER_ACCELERATION, _mov.Acceleration.Y, _mov.Acceleration.Z);
			}
			else
			{
				_mov.Acceleration = new Vector3(0, _mov.Acceleration.Y, _mov.Acceleration.Z);
			}
		}

		private void MoveRight()
		{
			if (_mov.Velocity.X < GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(GameConstant.PLAYER_ACCELERATION, _mov.Acceleration.Y, _mov.Acceleration.Z);
			}
			else
			{
				_mov.Acceleration = new Vector3(0, _mov.Acceleration.Y, _mov.Acceleration.Z);
			}
		}

		private void MoveUp()
		{
			if (_mov.Velocity.Y < GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, GameConstant.PLAYER_ACCELERATION, _mov.Acceleration.Z);
			}
			else
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, 0, _mov.Acceleration.Z);
			}
		}

		private void MoveDown()
		{
			if (_mov.Velocity.Y > -GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, -GameConstant.PLAYER_ACCELERATION, _mov.Acceleration.Z);
			}
			else
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, 0, _mov.Acceleration.Z);
			}
		}

		private void MoveBack()
		{
			if (_mov.Velocity.Z > -GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, _mov.Acceleration.Y, -GameConstant.PLAYER_ACCELERATION);
			}
			else
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, _mov.Acceleration.Y, 0);
			}
		}

		private void MoveFront()
		{
			if (_mov.Velocity.Z < GameConstant.PLAYER_MAX_SPEED)
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, _mov.Acceleration.Y, GameConstant.PLAYER_ACCELERATION);
			}
			else
			{
				_mov.Acceleration = new Vector3(_mov.Acceleration.X, _mov.Acceleration.Y, 0);
			}
		}
	}
}
